<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" >

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/turbolinks/5.2.0/turbolinks.js" integrity="sha256-iM4Yzi/zLj/IshPWMC1IluRxTtRjMqjPGd97TZ9yYpU=" crossorigin="anonymous"></script>
  <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Cousine|Inconsolata" rel="stylesheet">
  <link rel="shortcut icon" type="image/png" href="/favicon.png">

  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/css/css/fontawesome-all.min.css">
  <link rel="stylesheet" href="/css/css/work-sans-v7.css">
  <script src="/js/main.js"></script>

  <!-- Begin Jekyll SEO tag v2.6.1 -->
<title>PY: Get Delegations By User | Hive Developer</title>
<meta name="generator" content="Jekyll v4.0.0" />
<meta property="og:title" content="PY: Get Delegations By User" />
<meta property="og:locale" content="en_US" />
<meta name="description" content="How to get a list of active or expiring vesting delegations using Python." />
<meta property="og:description" content="How to get a list of active or expiring vesting delegations using Python." />
<link rel="canonical" href="get_delegations_by_user.html" />
<meta property="og:url" content="get_delegations_by_user.html" />
<meta property="og:site_name" content="Hive Developer" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2020-08-26T11:52:22-07:00" />
<script type="application/ld+json">
{"description":"How to get a list of active or expiring vesting delegations using Python.","mainEntityOfPage":{"@type":"WebPage","@id":"get_delegations_by_user.html"},"@type":"BlogPosting","url":"get_delegations_by_user.html","headline":"PY: Get Delegations By User","dateModified":"2020-08-26T11:52:22-07:00","datePublished":"2020-08-26T11:52:22-07:00","@context":"https://schema.org"}</script>
<!-- End Jekyll SEO tag -->


  
</head>
<body>
<header>
  <h1>
    <button type="button" class="open-nav" id="open-nav"></button>
    <a href="/"  class="logo-link"><img src="/images/logotype_black.svg" height="40" alt="Hive Developer logo"></a>
  </h1>

  
  <form action="/search/" method="get">
    <input type="text" name="q" id="search-input" placeholder="Search">
    <input type="submit" value="Search" style="display: none;">
  </form>
  

  <div id="sidebar" class="sidebar">
	
	
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/">
		<h6 class="ctrl-nav-section title">Introduction</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/#introduction-welcome">Welcome to Hive</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/quickstart/">
		<h6 class="ctrl-nav-section title">Quickstart</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/quickstart/#quickstart-choose-library">Choose Library</a></li>
						
					
						
						<li><a href="/quickstart/#quickstart-hive-full-nodes">Hive Nodes</a></li>
						
					
						
						<li><a href="/quickstart/#quickstart-testnet">Hive Testnet</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/tutorials/">
		<h6 class="ctrl-nav-section title">Tutorials</h6>
		<ul class="cont-nav-section content">
			
				
					
						<li><a href="/tutorials/#tutorials-javascript">Javascript</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-python">Python</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-ruby">Ruby</a></li>
					
				
					
						<li><a href="/tutorials/#tutorials-recipes">Recipes</a></li>
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/services/">
		<h6 class="ctrl-nav-section title">Services</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/services/#services-hive-blog">hive.blog</a></li>
						
					
						
						<li><a href="/services/#services-hivesigner">HiveSigner</a></li>
						
					
						
						<li><a href="/services/#services-jussi">Jussi</a></li>
						
					
						
						<li><a href="/services/#services-imagehoster">ImageHoster</a></li>
						
					
						
						<li><a href="/services/#services-dhf">DHF</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/apidefinitions/">
		<h6 class="ctrl-nav-section title">Appbase API</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/apidefinitions/#apidefinitions-condenser-api">Condenser Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-bridge">Bridge</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-account-by-key-api">Account By Key Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-account-history-api">Account History Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-block-api">Block Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-database-api">Database Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-debug-node-api">Debug Node Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-follow-api">Follow Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-jsonrpc">Jsonrpc</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-market-history-api">Market History Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-network-broadcast-api">Network Broadcast Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-rc-api">Rc Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-reputation-api">Reputation Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-rewards-api">Rewards Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-tags-api">Tags Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-transaction-status-api">Transaction Status Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-witness-api">Witness Api</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-broadcast-ops">Broadcast Ops</a></li>
						
					
						
						<li><a href="/apidefinitions/#apidefinitions-broadcast-ops-communities">Broadcast Ops Communities</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/resources/">
		<h6 class="ctrl-nav-section title">Resources</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/resources/#resources-overview">Overview</a></li>
						
					
						
						<li><a href="/resources/#resources-client-libs">Client Libraries</a></li>
						
					
						
						<li><a href="/resources/#resources-bluepaper">Bluepaper</a></li>
						
					
						
						<li><a href="/resources/#resources-whitepaper">Whitepaper</a></li>
						
					
						
						<li><a href="/resources/#resources-tools">Tools</a></li>
						
					
						
						<li><a href="/resources/#resources-hivesigner-libs">Hivesigner Libs</a></li>
						
					
						
						<li><a href="/resources/#resources-hive-keychain">Hive Keychain</a></li>
						
					
						
						<li><a href="/resources/#resources-developeradvocate">Community & Help</a></li>
						
					
				
			
		</ul>
	</section>
	
		
    <section class="pnl-main-nav-section main-nav-section" url="/glossary/">
		<h6 class="ctrl-nav-section title">Glossary</h6>
		<ul class="cont-nav-section content">
			
				
					
					
						
						<li><a href="/glossary/#glossary-chain-basics">Chain Basics</a></li>
						
					
						
					
						
						<li><a href="/glossary/#glossary-governance">Governance</a></li>
						
					
						
						<li><a href="/glossary/#glossary-transactions">Transactions</a></li>
						
					
						
						<li><a href="/glossary/#glossary-api">API</a></li>
						
					
						
						<li><a href="/glossary/#glossary-market">Market</a></li>
						
					
				
			
		</ul>
	</section>
	
</div>



  <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
</header>
<div class="main" id="top">
  <div class="hero">
    <h1 class="hero__h1">Hive Developer Portal </h1>
    <img class="hero__img" src="/images/honey-comb-92.png" style="position: relative; top: 10px;" />
  </div>
  <section id="tutorials-python-get-delegations-by-user" class="row tutorials-python-get-delegations-by-user">
	<h3>
		<a id="tutorials-python-get-delegations-by-user">
			PY: Get Delegations By User
			
		</a>
	</h3>
	
		<span class="description"><p>How to get a list of active or expiring vesting delegations using Python.</p>
</span>
	

	<p><span class="fa-pull-left top-of-tutorial-repo-link"><span class="first-word">Full</span>, runnable src of <a href="https://gitlab.syncad.com/hive/devportal/-/tree/master/tutorials/python/tutorials/29_get_delegations_by_user">Get Delegations By User</a> can be downloaded as part of: <a href="https://gitlab.syncad.com/hive/devportal/-/tree/master/tutorials/python">tutorials/python</a>.</span>
<br /></p>

<p>In this tutorial we will explain and show you how to pull a list of both active and expiring vesting delegations from the <strong>Steem</strong> blockchain using the <code class="highlighter-rouge">steem</code> class found within the <a href="https://github.com/steemit/steem-python">steem-python</a> library.</p>

<h2 id="intro">Intro</h2>

<p>The Steem python library has a built-in function to pull information from the blockchain. We are using the <code class="highlighter-rouge">get_vesting_delegations</code> and <code class="highlighter-rouge">get_expiring_vesting_delegations</code> methods found within the <code class="highlighter-rouge">steem</code> class in the library. Each of these functions are executed separately. It should be noted that when a delegation is cancelled the VESTS will only be available again after 7 days. The value of the delegation can also be changed at any time, either decreased or increased. To get active delegations we need the following parameters:</p>

<ol>
  <li><em>account</em> - The user account that the delegation list is being queried for</li>
  <li><em>from-account</em> - The account name from where to start the search. This parameter can be left empty to pull a list from the first delegatee</li>
  <li><em>limit</em> - The maximum amount of delegations that will be returned by the query</li>
</ol>

<p>The function to query the expiring delegations use the the same parameters except that the <code class="highlighter-rouge">start_account</code> is replaced by a <code class="highlighter-rouge">start_date</code>. If this value is greater than 7 days from present, it will always include all delegations that are pending expiration.</p>

<h2 id="steps">Steps</h2>

<ol>
  <li><a href="#setup"><strong>App setup</strong></a> - Library install and import. Connection to production</li>
  <li><a href="#input"><strong>User input</strong></a> - Input user and limit parameters</li>
  <li><a href="#query"><strong>Delegation lists</strong></a> - Selection of the type of list and blockchain query</li>
</ol>

<h4 id="1-app-setup-">1. App setup <a name="setup"></a></h4>

<p>In this tutorial we use 2 package:</p>

<ul>
  <li><code class="highlighter-rouge">steem</code> - steem-python library and interaction with Blockchain</li>
  <li><code class="highlighter-rouge">pick</code> - helps select the query type interactively</li>
</ul>

<p>We import the libraries and connect to the <code class="highlighter-rouge">production</code> server.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">from</span> <span class="nn">pick</span> <span class="kn">import</span> <span class="n">pick</span>
<span class="kn">from</span> <span class="nn">steem</span> <span class="kn">import</span> <span class="n">Steem</span>

<span class="kn">import</span> <span class="nn">pprint</span>

<span class="n">client</span> <span class="o">=</span> <span class="n">Steem</span><span class="p">()</span>
</code></pre></div></div>

<p><code class="highlighter-rouge">pprint</code> is used to print the query results in an easier to read format</p>

<h4 id="2-user-input-">2. User input <a name="input"></a></h4>

<p>The <code class="highlighter-rouge">account</code> and <code class="highlighter-rouge">limit</code> parameters are assigned via input from the console/terminal. We also check if the username provided does in fact exist on the blockchain using the <code class="highlighter-rouge">get_account</code> method also found within the <code class="highlighter-rouge">steem</code> class. This will return an null value if the name does not exist.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">#capture username
</span><span class="n">username</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">'Username: '</span><span class="p">)</span>

<span class="c1">#check username
</span><span class="n">result</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">get_account</span><span class="p">(</span><span class="n">username</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">result</span><span class="p">:</span>
	<span class="k">print</span><span class="p">(</span><span class="s">'Invalid username'</span><span class="p">)</span>
	<span class="nb">exit</span><span class="p">()</span>

<span class="c1">#capture list limit
</span><span class="n">limit</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">'Max number of vesting delegations to display: '</span><span class="p">)</span>
</code></pre></div></div>

<h4 id="3-delegation-lists-">3. Delegation lists <a name="query"></a></h4>

<p>We use two different functions to query active and expiring delegations, so the user is given a choice on which of these lists he wants to view.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">#list type
</span><span class="n">title</span> <span class="o">=</span> <span class="s">'Please choose the type of list: '</span>
<span class="n">options</span> <span class="o">=</span> <span class="p">[</span><span class="s">'Active Vesting Delegations'</span><span class="p">,</span> <span class="s">'Expiring Vesting Delegations'</span><span class="p">]</span>

<span class="c1">#get index and selected list name
</span><span class="n">option</span><span class="p">,</span> <span class="n">index</span> <span class="o">=</span> <span class="n">pick</span><span class="p">(</span><span class="n">options</span><span class="p">,</span> <span class="n">title</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'</span><span class="se">\n</span><span class="s">'</span> <span class="o">+</span> <span class="s">'List of '</span> <span class="o">+</span> <span class="n">option</span> <span class="o">+</span> <span class="s">': '</span> <span class="o">+</span> <span class="s">'</span><span class="se">\n</span><span class="s">'</span><span class="p">)</span>
</code></pre></div></div>

<p>Based on the result of the choice, the relevant blockchain query is executed and the result of the query displayed on the console/terminal.</p>

<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">if</span> <span class="n">option</span><span class="o">==</span><span class="s">'Active Vesting Delegations'</span> <span class="p">:</span>
    <span class="c1">#active delegations
</span>	<span class="n">delegations</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">get_vesting_delegations</span><span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="s">''</span><span class="p">,</span> <span class="n">limit</span><span class="p">)</span>
	<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">delegations</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
		<span class="k">print</span><span class="p">(</span><span class="s">'No '</span> <span class="o">+</span> <span class="n">option</span><span class="p">)</span>
	<span class="k">else</span><span class="p">:</span>
		<span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">delegations</span><span class="p">)</span>
<span class="k">else</span><span class="p">:</span>
    <span class="c1">#expiring delegations
</span>	<span class="n">delegations</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">get_expiring_vesting_delegations</span><span class="p">(</span><span class="n">username</span><span class="p">,</span> <span class="s">"2018-01-01T00:00:00"</span><span class="p">,</span> <span class="n">limit</span><span class="p">)</span>
	<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">delegations</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
		<span class="k">print</span><span class="p">(</span><span class="s">'No '</span> <span class="o">+</span> <span class="n">option</span><span class="p">)</span>
	<span class="k">else</span><span class="p">:</span>
		<span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="n">delegations</span><span class="p">)</span>
</code></pre></div></div>

<p>For both the queries the starting points were defined in such a way as to include all available data but this can be changed depending on the user requirements.</p>

<p>That’s it!</p>

<h3 id="to-run-the-tutorial">To Run the tutorial</h3>

<ol>
  <li><a href="getting_started.html">review dev requirements</a></li>
  <li><code class="highlighter-rouge">git clone https://gitlab.syncad.com/hive/devportal.git</code></li>
  <li><code class="highlighter-rouge">cd devportal/tutorials/python/29_get_delegations_by_user</code></li>
  <li><code class="highlighter-rouge">pip install -r requirements.txt</code></li>
  <li><code class="highlighter-rouge">python index.py</code></li>
  <li>After a few moments, you should see a prompt for input in terminal screen.</li>
</ol>

<hr />

</section>

</div>
<div class="footer">
  <a href="#">Back to top</a>
</div>
<script>
    document.getElementById("open-nav").addEventListener("click", function () {
        document.body.classList.toggle("nav-open");
    });
</script>
</body>
</html>


<script src="/js/adjust.js"></script>

